package com.yoi.SendAndRecv;

import com.rabbitmq.client.Channel;
import com.yoi.bean.Book;
import com.yoi.bean.Ordermessage;
import com.yoi.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
/**
 *
 * Version2  RabbitListener&RabbitHandler搭配使用
 */
@Component
//监听
/*@RabbitListener(bindings=@QueueBinding(value=@Queue(value="rabbtimq1-resource1-queue",arguments=@Argument(name="x-queue-type",value="quorum"),durable="true"),
        exchange=@Exchange(name="rabbitmq1-resource1-exchange",type="topic"), key="rabbitmq1.resource1.*"),containerFactory = "CustomerContainerFactory")*/
public class OrderRecv {

    @Autowired
    private OrderService orderService;

    //参数为Body
    //@RabbitHandler
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "rabbitmq2-resource2-queue", arguments = @Argument(name = "x-queue-type", value = "quorum"), durable = "true"),
            exchange = @Exchange(name = "rabbitmq2-resource2-exchange", type = "topic"), key = "rabbitmq2.resource2.*"), containerFactory = "Customer2ContainerFactory")
    public void onOrderMessageString(@Payload Book book, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        //消费者操作
        System.out.println("----收到新订单----");
        System.out.println("这里是消费者2接收到的订单数据：" + book.toString() + "-----");
        //System.out.println("订单ID:"+book.toString());
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //ACK手动签收操作
        channel.basicAck(deliveryTag, true);
        //生成订单
        SimpleDateFormat A = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Ordermessage order = new Ordermessage(book.getId(), book.getName(), "下单时间：" + A.format(new Date())+";消费者：Recv2");
        orderService.insertOrder(order);
        System.out.println("----已生成的订单详情----" + order.toString());

    }
}